/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.tags;

import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.brainstretch.models.quizzes.Clue;
import org.brainstretch.models.quizzes.ClueType;
import org.brainstretch.models.quizzes.Question;

/**
 *
 * @author mrodaitis
 */
public class QuestionTagHandler extends SimpleTagSupport {
    private Question question;

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        
        try {
            // TODO: insert code to write html before writing the body content.
            // e.g.:
            //
            // out.println("<strong>" + attribute_1 + "</strong>");
            // out.println("    <blockquote>");
            long questionId = question.getId();
            out.println("<div class = \"question\" id = \"question"+questionId+"\">");

            for(Clue clue: question.getClues()){
                writeClue(clue, out);
            }
            
            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }
            
            out.println("<div class = \"newClue\">");
            out.println("<select>");
            for(ClueType type: ClueType.values()){
                out.println("<option value = \""+type.ordinal()+"\">"+type.toString()+"</option>");
            }
            out.println("</select>");
            out.println("<input type = \"button\" value = \"New Clue\" id = \"newClueButton\"/>");
            out.println("</div>");
            
            out.println("</div>");

            // TODO: insert code to write html after writing the body content.
            // e.g.:
            //
            // out.println("    </blockquote>");

        } catch (java.io.IOException ex) {
            throw new JspException("Error in QuestionTagHandler tag", ex);
        }
    }

    
    public void setQuestion(Question question) {
        this.question = question;
    }

    private void writeClue(Clue clue, JspWriter out) throws IOException{
        switch(clue.getType()){
            case Text: 
                out.println("<textarea name = \"text\">");
                out.println(new String(clue.getBody()));
                out.println("</textarea>");
                break;
            case Audio:
            case Video:
                out.println("<label>Current file: </label>");
                out.println(clue.getUploadedFileName());
                break;
        }
    }
}
